//! \file WindowCharacter.cpp
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "Elements.h"
#include "GuiCEGUI.h"
#include "DnD.h"
#include <stddef.h>
#include <stdlib.h>
#include <iostream>

#include "Dice.h" // TODO: remove once rolled up to the DD library.

using namespace CEGUI;

// =====[ Local Definitions ]===================================================

// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================

// =====[ Local Functions ]=====================================================

// =====[ Private (Internal) Methods ]==========================================

// =====[ Local Implementation ]================================================








// Sample sub-class for ListboxTextItem that auto-sets the selection brush
// image.  This saves doing it manually every time in the code.
class MyListItem : public CEGUI::ListboxTextItem
{
public:
    MyListItem(const CEGUI::String& text) : ListboxTextItem(text)
    {
        setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
    }
};



// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
WindowCharacter::WindowCharacter(CEGUI::Window *pParent)
{
  // If no parent was requested, we'll attach to the root.
  if (pParent == NULL)
    { pParent = GuiCEGUI::getRoot(); }

  // Store our parent for future reference.
  m_pParent = pParent;

  // Load the console window layout and add it to the parent.
  pParent->addChildWindow(GuiCEGUI::getWm()->loadWindowLayout("dq_character.layout"));

  // TODO: stuff...

  Window* pWindow = static_cast<Window*>(GuiCEGUI::getWm()->getWindow("Character"));
  pWindow->setPosition(UVector2(UDim(0.01f, 0.0f), UDim(0.05f, 0.0f)));

//    Combobox* cbobox = static_cast<Combobox*>(GuiCEGUI::getWm()->getWindow("Dice/Combobox"));
//    // add items to the combobox list
//    cbobox->addItem(new MyListItem("1d6"));
//    cbobox->addItem(new MyListItem("1d8"));
//    cbobox->addItem(new MyListItem("1d10"));
//    cbobox->addItem(new MyListItem("1d12"));
//    cbobox->addItem(new MyListItem("1d20"));
//    cbobox->addItem(new MyListItem("1d100"));
//    cbobox->addItem(new MyListItem("1d6+1"));
//    cbobox->addItem(new MyListItem("1d6+2"));
//    cbobox->addItem(new MyListItem("1d8+1"));
//    cbobox->addItem(new MyListItem("1d8+2"));

  // Roll button.
  subscribeEvent("Character/Roll", PushButton::EventClicked, SubscriberSlot(&eventRoll));

//
//  ButtonBase* pButton = static_cast<ButtonBase*>(GuiCEGUI::getWm()->getWindow("Dice/Roll"));
////  Button* pButton = static_cast<Button*>(GuiCEGUI::getWm()->getWindow("Dice/Roll"));
//
//  //subscribeEvent("Dice/Roll", PushButton::EventClicked, SubscriberSlot(&eventRoll));
//  subscribeEvent("Dice/Roll", PushButton::EventClicked, pButton);


//  // Clear button.
//  subscribeEvent("Console/Clear", PushButton::EventClicked, SubscriberSlot(&eventClear));

}

//  WindowCharacter::~WindowCharacter()
//  {
//    // TODO Auto-generated destructor stub
//  }

//
//
//  // =============================================================================
//  // FxName
//  // -----------------------------------------------------------------------------
//  //! TODO: document
//  //!
//  //! \param [in] Arg
//  //!   TODO: document
//  //!
//  //! \retval Exception
//  //!   The success status of the requested operation.
//  //!   - eOKAY: Execution occurred as requested.
//  //!   .
//  //!
//  //! \b Example: \code
//  //! FxName(0);
//  //! \endcode
//  // -----------------------------------------------------------------------------
//  tERROR WindowCharacter::create(Window *pParent)
//  {
//  //  Window *pWindow;
//
//  // TODO: resolve this issue
//  //  // We can only create one dice window.
//  //  if (createSingle(pParent) == false)
//  //    { return (ERR_GUI_UNABLE_TO_CREATE); }
//
//    // If no parent was provided, we will attach it to the root.
//    if (pParent == NULL) { pParent = GuiCEGUI::getRoot(); }
//
//    // Load the console window layout and add it to the parent.
//    pParent->addChildWindow(GuiCEGUI::getWm()->loadWindowLayout("dq_dice.layout"));
//
//    Window* pWindow = static_cast<Window*>(GuiCEGUI::getWm()->getWindow("Dice"));
//
//    pWindow->setPosition(UVector2(UDim(0.25f, 0.0f), UDim(0.25f, 0.0f)));
//
//    Combobox* cbobox = static_cast<Combobox*>(GuiCEGUI::getWm()->getWindow("Dice/Combobox"));
//    // add items to the combobox list
//    cbobox->addItem(new MyListItem("1d6"));
//    cbobox->addItem(new MyListItem("1d8"));
//    cbobox->addItem(new MyListItem("2d8"));
//    cbobox->addItem(new MyListItem("1d20"));
//    cbobox->addItem(new MyListItem("1d6+2"));
//    cbobox->addItem(new MyListItem("1d6+1d8"));
//
//
//    // Roll button.
//    subscribeEvent("Dice/Roll", PushButton::EventClicked, SubscriberSlot(&eventRoll));
//
//  //  // Clear button.
//  //  subscribeEvent("Console/Clear", PushButton::EventClicked, SubscriberSlot(&eventClear));
//
//    // Return success.
//    return (ERR_NONE);
//  }


bool WindowCharacter::eventRoll(const CEGUI::EventArgs& Event)
{
  using namespace DnD;

  static Abilities* pAbilities = NULL;
  std::string Text;

  // Initialize the abilities if they don't already exist.
  if (pAbilities == NULL)
    { pAbilities = new Abilities(); }

  // Do the requested roll.
  pAbilities->rollNew();

  // Send the results to the console.
  WindowConsole::getSingletonPtr()->appendText("-----");
  WindowConsole::getSingletonPtr()->appendText("STR: " + pAbilities->getStrText());
  WindowConsole::getSingletonPtr()->appendText("DEX: " + pAbilities->getDexText());
  WindowConsole::getSingletonPtr()->appendText("CON: " + pAbilities->getConText());
  WindowConsole::getSingletonPtr()->appendText("INT: " + pAbilities->getIntText());
  WindowConsole::getSingletonPtr()->appendText("WIS: " + pAbilities->getWisText());
  WindowConsole::getSingletonPtr()->appendText("CHA: " + pAbilities->getChaText());

  // Return the event was handled.
  return (true);
}



